package com.leyou.sms.task;

import com.leyou.client.AuthClient;
import com.leyou.sms.config.JwtProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class PrivilegeTokenHolder {

    @Autowired
    private JwtProperties prop;

    private String token;

    @Autowired
    private AuthClient authClient;

    /**
     * token刷新间隔
     */
    private static final long TOKEN_REFRESH_INTERVAL = 86400000L;

    /**
     * token获取失败后重试的间隔
     */
    private static final long TOKEN_RETRY_INTERVAL = 10000L;


    @Scheduled(fixedDelay = TOKEN_REFRESH_INTERVAL)
    public void loadToken() throws InterruptedException {
      while (true){
          try {
              token = authClient.authorize(prop.getApp().getId(), prop.getApp().getSecret());
              log.info("【网关】定时获取token成功");
              break;
          } catch (Exception e) {
              e.printStackTrace();
              log.info("【网关】定时获取token失败");
          }
          Thread.sleep(TOKEN_RETRY_INTERVAL);
      }

    }
    public String getToken(){
        return token;
    }


}
